package com.njmaomao.gj.pyot.domain.wechat.wechatapi.refund;

import com.njmaomao.gj.pyot.config.Bizs;
import com.njmaomao.gj.pyot.domain.wechat.PayConfig;
import com.njmaomao.gj.pyot.domain.model.PayRecord;
import com.thoughtworks.xstream.annotations.XStreamAlias;

/**
 * 申请退款入参
 *
 * @author hh 2018-5-10
 */
@XStreamAlias("xml")
public class RefundReq {

    // 公众账号ID 必填[是] 微信分配的公众账号ID（企业号corpid即为此appId）
    private String appid;

    // 商户号 必填[是] 微信支付分配的商户号
    @XStreamAlias("mch_id")
    private String mchId;

    // 随机字符串 必填[是] 随机字符串，不长于32位。推荐随机数生成算法
    @XStreamAlias("nonce_str")
    private String nonceStr;

    // 签名 必填[是] 签名，详见签名生成算法
    private String sign;

    // 签名类型 必填[否] 签名类型，目前支持HMAC-SHA256和MD5，默认为MD5
    @XStreamAlias("sign_type")
    private String signType;

    // 微信订单号 必填[二选一] 商户订单号，在支付通知中有返回
    @XStreamAlias("transaction_id")
    private String transactionId;

    // 商户订单号 必填[二选一] 微信订单号
    @XStreamAlias("out_trade_no")
    private String outTradeNo;

    // 商户退款单号 必填[是] 商户系统内部的退款单号，商户系统内部唯一，只能是数字、大小写字母_-|*@ ，同一退款单号多次请求只退一笔。
    @XStreamAlias("out_refund_no")
    private String outRefundNo;

    // 订单金额 必填[是] 订单总金额，单位为分，只能为整数，详见支付金额
    @XStreamAlias("total_fee")
    private int totalFee;

    // 退款金额 必填[是] 退款总金额，订单总金额，单位为分，只能为整数，详见支付金额
    @XStreamAlias("refund_fee")
    private int refundFee;

    // 退款货币种类 必填[否] 退款货币类型，需与支付一致，或者不填。符合ISO 4217标准的三位字母代码，默认人民币：CNY，其他值列表详见货币类型
    @XStreamAlias("refund_fee_type")
    private String refundFeeType;

    // 退款原因 必填[否] 若商户传入，会在下发给用户的退款消息中体现退款原因
    @XStreamAlias("refund_desc")
    private String refundDesc;

    /**
     * 退款资金来源 必填[否]
     * 仅针对老资金流商户使用
     * REFUND_SOURCE_UNSETTLED_FUNDS---未结算资金退款（默认使用未结算资金退款）
     * REFUND_SOURCE_RECHARGE_FUNDS---可用余额退款
     */
    @XStreamAlias("refund_account")
    private String refundAccount;

    /**
     * 退款结果通知url 必填[否]
     * 异步接收微信支付退款结果通知的回调地址，通知URL必须为外网可访问的url，不允许带参数
     * 如果参数中传了notify_url，则商户平台上配置的回调地址将不会生效。
     */
    @XStreamAlias("notify_url")
    private String notifyUrl;

    public String getAppid() {
        return appid;
    }

    public String getMchId() {
        return mchId;
    }

    public String getNonceStr() {
        return nonceStr;
    }

    public String getSign() {
        return sign;
    }

    public String getSignType() {
        return signType;
    }

    public String getTransactionId() {
        return transactionId;
    }

    public String getOutTradeNo() {
        return outTradeNo;
    }

    public String getOutRefundNo() {
        return outRefundNo;
    }

    public int getTotalFee() {
        return totalFee;
    }

    public int getRefundFee() {
        return refundFee;
    }

    public String getRefundFeeType() {
        return refundFeeType;
    }

    public String getRefundDesc() {
        return refundDesc;
    }

    public String getRefundAccount() {
        return refundAccount;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public static RefundReq build(PayConfig payConfig, String tradeNo, String refundOrderCode, Integer refundFee, PayRecord payRecord) {
        RefundReq refundReq = new RefundReq();
        refundReq.appid = payConfig.getAppid();
        refundReq.mchId = payConfig.getMchId();
        refundReq.nonceStr = Bizs.generateNonceStr();
        refundReq.signType = Bizs.SIGN_TYPE;
        refundReq.transactionId = tradeNo;
        refundReq.outTradeNo = payRecord.getOrderCode();
        refundReq.outRefundNo = refundOrderCode;
        refundReq.totalFee = payRecord.getTotalAmount();
        refundReq.refundFee = refundFee;
        refundReq.refundFeeType = "CNY";
//        refundReq.refundDesc =
//        refundReq.refundAccount =
        refundReq.notifyUrl = payConfig.getRefundNotifyUrl();
        refundReq.sign = Bizs.getMd5Sign(refundReq, payConfig.getKey());
        return refundReq;
    }
}
